<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Users extends Base{
    //申请代理
    public function userEdit(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.username"))){
            $data['username']=input("param.username");
        }else{
            return json(['code' => 502, 'msg' => '请输入姓名']);
        }
        if(!empty(input("param.tel_phone"))){
            $data['tel_phone']=input("param.tel_phone");
        }else{
            return json(['code' => 503, 'msg' => '请输入电话']);
        }
        if(!empty(input("param.id_card"))){
            $data['id_card']=input("param.id_card");
        }else{
            return json(['code' => 504, 'msg' => '请输入身份证号']);
        }
        if (input("param.sex")==1||input("param.sex")==2) {
            $data['sex']=input("param.sex");
        }else{
            return json(['code' => 505, 'msg' => '请选择性别']);
        }
        if(!empty(input("param.address"))){
            $data['address']=input("param.address");
        }else{
            return json(['code' => 506, 'msg' => '请输入地址']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        if(isset($_FILES['avatar'])){
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($_FILES['avatar']['tmp_name'],$imgpath.$imgsubpath.$_FILES['avatar']['name']);
            $avatar="/uploads/images/".$imgsubpath.$_FILES['avatar']['name'];
            $data['avatar']=$avatar;
            $data['uptime']=time();
            $res=Db::name('users')->where('id',$uid)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功！']);
            }else{
                return json(['code' => 500, 'msg' => '编辑失败！']);
            }
        }else{
            $data['uptime']=time();
            $res=Db::name('users')->where('id',$uid)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功！']);
            }else{
                return json(['code' => 500, 'msg' => '编辑失败！']);
            }
        }
    }
}